package com.longjianghuasheng.logistics.business.home

import android.annotation.SuppressLint
import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import androidx.core.view.get
import androidx.fragment.app.viewModels
import com.blankj.utilcode.util.LogUtils
import com.longjianghuasheng.logistics.R
import com.longjianghuasheng.logistics.base.BaseMultiStateFragment
import com.longjianghuasheng.logistics.business.home.adapter.HomeAdapter
import com.longjianghuasheng.logistics.business.mine.viewmodel.MineViewModel
import com.longjianghuasheng.logistics.databinding.FragmentHomeBinding
import com.longjianghuasheng.logistics.eventViewModel
import com.longjianghuasheng.logistics.ext.*
import com.xuexiang.xui.widget.actionbar.TitleBar


class HomeFragment : BaseMultiStateFragment<HomeViewModel, FragmentHomeBinding>() {


    lateinit var titleBar: TitleBar

    override fun isRefresh(): Boolean = true

    override fun isLoadMore(): Boolean = true

    private val adapter by lazy {
        HomeAdapter()
    }
    private val mineViewModel by viewModels<MineViewModel>()

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val flTop = view.findViewById<FrameLayout>(R.id.suspension_top)

        val includeTitle =
            LayoutInflater.from(requireContext()).inflate(R.layout.include_title, flTop)
        titleBar = includeTitle.findViewById(R.id.title_bar)
        titleBar.disableLeftView()
        titleBar.setTitle("首页").centerText.typeface = Typeface.defaultFromStyle(Typeface.BOLD)
        titleBar.alpha = 0F
        super.onViewCreated(view, savedInstanceState)
    }

    override fun layoutId(): Int = R.layout.fragment_home

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)

        adapter.onTabListener = { id ->


            eventViewModel.toFreightEvent.value = id

        }
        adapter.onNewsListener = {

            navigate(R.id.newsFragment, Bundle().apply {
                putInt("id", it)
            })
        }
        adapter.onServiceListener = {

            mineViewModel.hot_line()
        }



        mDatabind.recyclerView.initLinerDecoration(space = 0F, adapter = adapter)
            .setHasFixedSize(false)

        mStatefulLayout.showLoading("")
    }


    private var bannerHeight = 0
    private var bannerTop = 0
    private fun FragmentHomeBinding.initAlpha() {

        recyclerView.viewTreeObserver.addOnGlobalLayoutListener {
            recyclerView.viewTreeObserver.removeOnGlobalLayoutListener {}

            bannerHeight = recyclerView[1].height
            bannerTop = recyclerView[1].top
            scrollView.setScrollViewListener { _, _, newy, _, _ ->
                //这个就是关键方法了，大家可根据实际需要进行编写

//                titleBar.alpha = if (newy <= firstItemHeight) 0F else 1F

                when {

                    newy <= bannerTop -> {
                        titleBar.alpha = 0F

                    }
                    newy in 1..bannerHeight -> {
                        getPercentage(newy.toFloat(), bannerHeight.toFloat()).let {
                            titleBar.alpha = it
                        }
                    }

                    else -> {
                        titleBar.alpha = 1F
                    }

                }

            }
        }


    }


    @SuppressLint("NotifyDataSetChanged")
    override fun createObserver() {
        super.createObserver()
        mViewModel.convertResult.observer(this) {


            if (mViewModel.page == 1) {

                adapter.setNewInstance(it)

                if (adapter.isFirst) {
                    mDatabind.initAlpha()
                }

                requireActivity().getLocation {

                    mViewModel.city = it?.city ?: ""

                    adapter.data.forEach {
                        it.city = mViewModel.city
                    }
                    adapter.notifyDataSetChanged()

                    LogUtils.d("location", it)
                }


                return@observer
            }

            val datas = it[0].newsResponse.data
            if (datas.isNullOrEmpty()) {
                mViewModel.page--
                smartLayout.setNoMoreData(true)
                return@observer
            }

            adapter.newsAdapter.addData(datas)


        }

        mineViewModel.resultHot_line.observe(this) {
            openPhone(it)
        }

    }

    override fun initData() {
        super.initData()
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        mViewModel.getHome()


    }

}